layui.use(['layer','form','table','element','layedit','upload'],function () {
    var layer = layui.layer
        ,form = layui.form
        ,table =layui.table
        ,element = layui.element
        ,layedit = layui.layedit
        ,upload = layui.upload;

    var labelId = $("#id").val();
    //动态加载type和label
    $.ajax({
        type:"GET",
        url:"/admin/label/getLabelAndTypeAndSelected/"+labelId,
        success:function (result) {
            var typeList = result.data.labelTypeList.typeList;

            $("#types").empty();
            $.each(typeList,function (i,type) {
                var typeName = type.typeName;
                var id = type.id;
                var isSeleted = type.selected;

                //过滤免费项目无关的type
                if(typeName != "首页" && typeName != "包赔项目" && typeName != "投资赚钱" && typeName != "靠谱项目" && typeName !="最新项目") {

                    if (isSeleted) {
                        $("#types").append("<input type='radio'  checked='' name='type' title='" + typeName + "' value='" + id + "'></input>");
                    } else {
                        $("#types").append("<input type='radio'  name='type' title='" + typeName + "' value='" + id + "'></input>");
                    }
                }
            });

            //重新渲染下拉框
            form.render();
        },
        error:function () {
            layer.msg("系统错误！");
        }
    });

        //提交表单
    form.on('submit(form)',function (data) {
        var paramData = {
            "labelName":$("#labelName").val(),
            "keys":$("#keys").val(),
            "isShowTitle":$('input[name="isShowTitle"]:checked ').val(),
            "typeId":$("input[name=type]:checked").val()
        };

        var data2 = JSON.stringify(paramData);

        //保存项目
        $.ajax({
            url:"/admin/label/update",
            type:"POST",
            data:data2,
            // dataType:"json",
            contentType:"application/json;charset=utf-8",
            success:function (result) {
                // console.log("data=="+form_data)
                if(result.flag){
                    layer.msg(result.message);
                    //清空内容
                    // $("#form").reset();
                    document.getElementById("form").reset();
                    layui.form.render();
                }else{
                    layer.msg(result.message);
                }
            },
            error:function () {
                layer.msg("系统错误！");
            }
        });

    });
});
